package com.innovation.action.player;

import com.innovation.action.BaseAction;
import com.innovation.cache.PlayerCache;
import com.innovation.core.RequestSession;
import com.innovation.data.model.Player;
import com.innovation.message.player.LoginReqProto.LoginReq;
import com.innovation.message.player.LoginRespProto.LoginResp;
import com.innovation.message.player.PlayerMsgProto.PlayerMsg;
import com.innovation.service.DataPushService;
import com.innovation.service.PlayerService;
import com.innovation.util.TimeUtil;

/**
 * 登录操作
 * @author Administrator
 *
 */
public class LoginAction extends BaseAction {

	@Override
	protected void done(RequestSession reqSession) throws Exception {
		LoginReq req = LoginReq.parseFrom(reqSession.getRequest().getData());

		Player player = PlayerService.login(req.getOpenId().trim().toLowerCase());
		if (player == null) {
			player = PlayerService.createPlayer(req.getOpenId(), req.getName());
			
			//加入到在线列表中
			PlayerCache.INSTANCE.getOnlineMap().put(player.getId(), player);
			PlayerCache.INSTANCE.getPlayerSet().put(player.getId(), player);
		}else{
			player.setLastLoginTime(player.getLoginTime());
			player.setLoginTime(TimeUtil.now());
			PlayerService.savePlayer(player);
		}
		reqSession.setPlayer(player);
		//设置日志信息
		initLogData(reqSession);
		
		//调用一下access方法,重置一些必须数据
		access(reqSession);
		
		player.getDataMsg().clear();
//		
//		//推送玩家英雄数据
//		PlayerHero playerHero = player.getPlayerHero();
//		if(playerHero != null){
//			DataPushService.pushHeroMsg(player, playerHero.getHeros());
//		}
//		
//		//推送道具数据
//		PlayerItem playerItem = player.getPlayerItem();
//		if(playerItem != null){
//			DataPushService.pushItemMsg(player, playerItem.getItems());
//		}
//		
//		//推送玩家任務数据
//		PlayerMission playerMission = player.getPlayerMission();
//		if(playerMission != null){
//			DataPushService.pushMissionMsg(player, playerMission.getMissions());
//		}
//		
//		//推送玩家消息数据
//		PlayerMessage playerMessage = player.getPlayerMessage();
//		if(playerMessage != null){
//			List<Bulletin> bulletins = playerMessage.getBulletins();
//			//按显示顺序排好序
//			Collections.sort(bulletins, new Comparator<Bulletin>() {
//				@Override
//				public int compare(Bulletin bulletin1, Bulletin bulletin2) {
//					ConfigBulletin configBulletin1 = ConfigHelper.getConfigBulletin(bulletin1.getId());
//					ConfigBulletin configBulletin2 = ConfigHelper.getConfigBulletin(bulletin2.getId());
//					if(configBulletin1.getDisplayOrder() > configBulletin2.getDisplayOrder()){
//						return 1;
//					}else{
//						return -1;
//					}
//				}
//			});
//			DataPushService.pushBulletinMsg(player, playerMessage.getBulletins());
//			
//			List<Message> msgs = playerMessage.getMessages();
//			DataPushService.pushMessageMsg(player, msgs);
//		}
//		
		//推送玩家数据给客户端
		DataPushService.pushPlayerMsg(player);
		
		//登录返回信息
		LoginResp.Builder body = LoginResp.newBuilder();
		//玩家信息
		PlayerMsg.Builder playerMsg = PlayerMsg.newBuilder();
		player.write2Proto(playerMsg);
		body.setPlayer(playerMsg);
		reqSession.setResponseMsg(body.build());
	}
	
}
